package ru.spb.osll.myfit.server.execute;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import org.easymock.EasyMock;
import org.junit.Test;

import ru.spb.osll.myfit.common.PageExecutingResult;
import ru.spb.osll.myfit.common.ProgressCounts;
import ru.spb.osll.myfit.common.Table;
import ru.spb.osll.myfit.common.PageExecutingResult.PageExecutingState;

public class PageExecutorTest {
	@Test
	public void testPageExecutorRun() throws Exception {
		FitNesseTestRunner runner = EasyMock
				.createMock(FitNesseTestRunner.class);
		FixtureListenerImpl fixtureListener = EasyMock
				.createMock(FixtureListenerImpl.class);
		PageWriter pageWriter = EasyMock.createMock(PageWriter.class);
		runner.run("TestPage");
		EasyMock.expectLastCall();

		ArrayList<Table> tables = new ArrayList<Table>();
		tables.add(new Table());
		ProgressCounts counts = new ProgressCounts();
		EasyMock.expect(fixtureListener.getTables()).andReturn(tables);
		EasyMock.expect(fixtureListener.getCounts()).andReturn(counts);
		EasyMock.expect(fixtureListener.getOutputStream()).andReturn(
		    new ByteArrayOutputStream());
		
		EasyMock.replay(runner, fixtureListener);
		

		PageExecutor executor = new PageExecutor(runner, "TestPage",
				fixtureListener, pageWriter);

		executor.run();
		PageExecutingResult result = executor.getPageExecutingResult();
		assertEquals(PageExecutingState.FINISH, result.getState());
		assertFalse(result.getExecutedTables().isEmpty());
		EasyMock.verify(runner, fixtureListener);
	}
}
